package kelisPkg.loadPackage
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	import flash.utils.getQualifiedSuperclassName;
	
	
	

	public class MaterialsManager extends EventDispatcher
	{
		public function MaterialsManager()
		{
		}
		static private var _instance:MaterialsManager;
		static public function get instance():MaterialsManager{
			_instance=_instance||new MaterialsManager;
			return _instance;
		}
		private var _pngLoader:ImgLoader=new ImgLoader;
		private var _xmlLoader:XmlLoader=new XmlLoader;
		private var _pngLoaderContext:LoaderContext;
		private var _thisUrl:String;
		public function start():void{
			_pngLoaderContext=new LoaderContext;
			_pngLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,_loadOk);
			_pngLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,_error);
			_pngLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,_progress);
			
			_xmlLoader.addEventListener(Event.COMPLETE,_loadXmlOk);
			_xmlLoader.addEventListener(IOErrorEvent.IO_ERROR,_xmlIOErr)
		}
		private function _loadXmlOk(e:Event):void
		{
			_pngLoader.load(new URLRequest(_xmlLoader.xml.@imagePath),_pngLoaderContext);
		}
		private function _xmlIOErr(e:IOErrorEvent):void
		{
			trace("加载("+_thisUrl+")资源失败");
		}
		public function addUI(url:String):void
		{
			_thisUrl=url;
			_xmlLoader.load(new URLRequest(url));
		}
		private function _loadOk(e:Event):void{
			var obj:Object=new Object;
			obj.url=_thisUrl
			 obj.xml=_xmlLoader.xml;
			 obj.texture=_pngLoader.texture;
			 TexturesManager.instance.addNewMaterial(obj);
			dispatchEvent(new LoadEvent(LoadEvent.LOAD_COMPLETE,obj));
		}
		private function _progress(e:ProgressEvent):void
		{
			var ix:Number=e.bytesTotal;
			var iy:Number=e.bytesLoaded;
			var iz:Number=ix*100/iy;
			dispatchEvent(new LoadEvent(LoadEvent.LOAD_PROGRESS,iz));
		}
		private function _error(e:IOErrorEvent):void
		{
			trace("加载("+_thisUrl+")资源失败");
		}
		
		

		
		
		
		
		
		public function buildClass(className:String):Class{
			var obj:Class=_pngLoaderContext.applicationDomain.getDefinition(className) as Class;
			return obj;
		}
		public function buildObj(className:String):Object{
			var reObj:Object=new (buildClass(className))
			return reObj;
		}

		public function buildmoduleByStrName(str:String):Object
		{
			var comp:Object = new (returnAppClass(str));
			return comp;
		}
		
		public function returnAppClass(className:String):Class{
			return Class(getDefinitionByName(className));
		}
	}
}